package com.weilx959.spt.core.component;

import com.jfoenix.controls.JFXDialog;
import com.weilx959.spt.core.RootPaneContext;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 加载等待弹窗
 *
 * @author weilx
 * @since 1.0.2
 */
@Slf4j
public class VLoading {
    private static final VLoading INSTANCE = new VLoading();

    @Getter
    private final JFXDialog dialog;

    /**
     * {@code dialog} 展示的消息内容
     */
    private final StringProperty message = new SimpleStringProperty(this, "MESSAGE");

    private VLoading() {

        Label bgLabel = new Label();
        bgLabel.getStyleClass().add("v-loading-icon");

        Label text = new Label();
        text.getStyleClass().add("v-loading-text");
        message.addListener(((observable, oldValue, newValue) -> text.setText(newValue)));

        VBox content = new VBox(2, bgLabel, text);
        content.getStyleClass().add("v-loading-content");
        VBox.setVgrow(text, Priority.ALWAYS);

        dialog = new JFXDialog(RootPaneContext.get(), content, JFXDialog.DialogTransition.NONE, false);

        for (Node child : dialog.getChildren()) {
            child.setStyle("-fx-background-color: transparent");
        }
    }

    public static void start() {
        VLoading.start(null);
    }

    /**
     * 展示加载信息
     * @param message 提示信息
     */
    public static void start(String message) {
        log.info("loading start");
        VLoading.INSTANCE.message.set(message);
        VLoading.INSTANCE.dialog.show();
    }

    /**
     * 关闭加载
     */
    public static void end() {
        log.info("loading end");
        VLoading.INSTANCE.dialog.close();
    }
}
